package com.chidopi.market.aa.dao.impl;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;

import com.chidopi.cdf.dao.AbstractBaseDaoImpl;
import com.chidopi.market.aa.domain.Accounts;

public class AccountDaoImpl extends AbstractBaseDaoImpl<Accounts> {
	private static Logger logger = LoggerFactory.getLogger(AccountDaoImpl.class);

	public List<Accounts> find(String sqlWhere, Object... args) {
		if (sqlWhere == null || sqlWhere.trim().equals("")) {
			String sql = "select * from accounts ";
			return super.simpleJdbcTemplate.query(sql, this.getRowMapper());
		} else {
			logger.debug("sqlWhere:[" + sqlWhere + "]/params:" + Arrays.asList(args).toString());
			String sql = "select * from accounts where " + sqlWhere;
			List<Accounts> list = super.simpleJdbcTemplate.query(sql, this.getRowMapper(), args);
			if (list == null) {
				logger.debug("query null!!!");
			} else {
				for (Accounts acc : list) {
					logger.debug("id=" + acc.getId() + "/username=" + acc.getUsername());
				}
			}
			return list;
			// return super.simpleJdbcTemplate.query(sql, this.getRowMapper(), args);
		}
	}

	public Accounts findById(Object id) {
		String sql = "id=?";
		List<Accounts> list = this.find(sql, id);
		if (0 == list.size()) return null;
		return list.get(0);
	}

	public int insert(Accounts t) {
		String sql = "insert into accounts (username, NAME_TW, NAME_EN, EMAIL, PASSWORD, EFFECTIVE_START_DATE, EFFECTIVE_END_DATE, LOCK_START_DATE, LOCK_END_DATE, ENABLE_FLAG, LOGIN_ERR_TOP, LOGIN_ERR_NUM, LOGIN_FLAG, CREATE_DATE, CREATE_BY) "
				+ "values (:username, :name_tw, :name_en, :email, :password, :effective_start_date, :effective_end_date, :lock_start_date, :lock_end_date, :enable_flag, :login_err_top, :login_err_num, :login_flag, NOW(), :create_by)";
		return super.execSQL(sql, t);
	}

	public int update(Accounts t) {
		String sql = "update accounts set username = :username, name_tw = :name_tw, name_en = :name_en, email = :email, effective_start_date = :effective_start_date, effective_end_date = :effective_end_date, lock_start_date = :lock_start_date, lock_end_date = :lock_end_date, enable_flag = :enable_flag, login_err_top = :login_err_top, login_err_num = :login_err_num, login_flag = :login_flag, update_by = :update_by where id= :id";
		return super.execSQL(sql, t);
	}

	public int delete(Accounts t) {
		logger.debug("Delete:" + t.getId());
		String sql = "delete from accounts where id = :id";
		return super.execSQL(sql, t);
	}

	@Override
	public String getSequenceName() {
		return "Accounts_SEQ";
	}

	@Override
	public String getTable() {
		return "Accounts";
	}

	@Override
	protected RowMapper<Accounts> getRowMapper() {
		return ParameterizedBeanPropertyRowMapper.newInstance(Accounts.class);
//		return new ParameterizedRowMapper<Accounts>() {
//            public Accounts mapRow(ResultSet rs, int rowNum) throws SQLException {
//            	Accounts vo = new Accounts();
//            	vo.setId(rs.getInt("ID"));
//                vo.setUsername(rs.getString("USERNAME"));
//                vo.setName_tw(rs.getString("NAME_TW"));
//                vo.setName_en(rs.getString("NAME_EN"));
//                vo.setEmail(rs.getString("EMAIL"));
//                vo.setEffective_start_date(rs.getDate("EFFECTIVE_START_DATE"));
//                vo.setEffective_end_date(rs.getDate("EFFECTIVE_END_DATE"));
//                vo.setLock_start_date(rs.getDate("LOCK_START_DATE"));
//                vo.setLock_end_date(rs.getDate("LOCK_END_DATE"));
//                vo.setEnable_flag(rs.getString("ENABLE_FLAG"));
//                vo.setLogin_err_top(rs.getInt("LOGIN_ERR_TOP"));
//                vo.setLogin_err_num(rs.getInt("LOGIN_ERR_NUM"));
//                vo.setLogin_flag(rs.getString("LOGIN_FLAG"));
//                vo.setCreate_date(rs.getDate("CREATE_DATE"));
//                vo.setCreate_by(rs.getInt("CREATE_BY"));
//                vo.setUpdate_date(rs.getDate("UPDATE_DATE"));
//                vo.setUpdate_by(rs.getInt("UPDATE_BY"));
//                return vo;
//            }
//        };
	}
}
